package com.hzg.ssm.web.controller;

import com.hzg.ssm.domain.Employee;
import com.hzg.ssm.query.EmployeeQueryObject;
import com.hzg.ssm.query.QueryObject;
import com.hzg.ssm.service.IDepartmentService;
import com.hzg.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ Description: TODO
 * @ Author: 卓哥
 * @ Date: 2020/7/5 18:46
 * @ Version: 1.0
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("list")
    public String pageList(Model model, @ModelAttribute("qo") EmployeeQueryObject queryObject){
        model.addAttribute("pageResult", employeeService.query(queryObject));
        System.out.println("keyword===="+queryObject.getKeyword());
        model.addAttribute("departments", departmentService.selectAll());
        return "employee/list";
    }
    @RequestMapping("delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }
    @RequestMapping("input")
    public String input(Long id,Model model){

        if (id!=null){
            model.addAttribute("employee",employeeService.select(id) );
        }
        model.addAttribute("departments", departmentService.selectAll());
        return "employee/input";
    }
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Employee employee){
        System.out.println(employee.getId());
        if (employee.getId()!=null){
            employeeService.update(employee);
        }else {

            employeeService.insert(employee);
        }
        return "redirect:/employee/list.do";
    }
}
